<template>
  <div class="login-container">
    <!-- 表单 -->
    <!-- mobel：表单的数据-->
    <!-- rules表单规则 -->
    <!-- label-position：设置左对齐 -->
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <!-- 头部 -->
      <div class="title-container">
        <h3 class="title">
          <img src="../../assets/common/login-logo.png" alt="">
        </h3>
      </div>

      <el-form-item prop="mobile">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>

        <el-input
          ref="mobile"
          v-model="loginForm.mobile"
          placeholder="手机号"
          name="mobile"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>
      <!-- prop规定表单的类型 -->
      <el-form-item prop="password">
        <span class="svg-container">
          <!-- icon图标 -->
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>

      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleLogin"
      >登陆</el-button>
      <!-- 底部注册 -->
      <div class="tips">
        <!-- <span style="margin-right: 20px">username: admin</span> -->
        <a> 没有账号？点击注册</a>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      // 数据
      loginForm: {
        mobile: '13800000002',
        password: '123456'
      },
      // 表单校验规则
      loginRules: {
        mobile: [
          // 是否是必须项     触发时间：失去焦点时  提示信息
          { required: true, trigger: 'blur', message: '不能为空' },
          {
            // 正则验证
            pattern:
              /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/,
            trigger: 'blur',
            message: '手机号错误'
          }
        ],
        password: [
          { required: true, trigger: 'blur', message: '不能为空' },
          { min: 6, trigger: 'blur', message: '最小六位数' }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  // 深度监听
  watch: {
    // 监测对象是路有对象
    $route: {
      handler: function(route) {
        // 逻辑运算符短路运算，如果query有值，则继续执行下一步
        this.redirect = route.query && route.query.redirect
      },
      // 立即启动
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      // 校验规则

      this.$refs.loginForm.validate(async(valid) => {
        if (valid) {
          // 在vuex中发起请求

          await this.$store.dispatch('user/loginAction', this.loginForm)

          if (this.redirect) {
            this.$router.push({
              path: this.redirect
            })
          } else {
            this.$router.push({
              path: '/'
            })
          }
        } else {
          this.$message.error('登陆失败')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #bac9de;
$cursor: #bac9de;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;
      .el-form-item__error {
        color: #fff !important;
      }
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: #d2e2fa;
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  background: url('https://cn.bing.com/th?id=OHR.FallAssiniboine_ZH-CN8878734090_1920x1080.jpg&rf=LaDigue_1920x1080.jpg'),
    no-repeat, center;
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    text-align: center;
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
